package images

import (
	"context"

	"server/internal/common/biz"
	"server/internal/svc"
	"server/internal/utils"

	"github.com/docker/docker/api/types/filters"
	"github.com/zeromicro/go-zero/core/logx"
)

type PruneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// prune image
func NewPruneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PruneLogic {
	return &PruneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PruneLogic) Prune() (resp bool, err error) {

	pruneReport, err := l.svcCtx.DockerCli.ImagesPrune(l.ctx, filters.NewArgs())
	if err != nil {
		l.Logger.Errorf("prune image error: %v", err)
		return false, biz.ImagePruneError
	}

	l.Debug(utils.PrintLog("prune image report: ", pruneReport))
	return true, nil
}
